home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / FREENET / FERGUSON / CLIENT_P / !Client_Ex / c / Address next >
Text File  |  1995-06-05  |  5KB  |  165 lines

  1. /*
  2.  
  3. File:               Address.c
  4. Date:               31-May-1995
  5. Author:             © Duncan Ferguson
  6. File Purpose:       provide window to get server address with
  7. Version:            0.01
  8.  
  9. */
  10.  
  11. /* handle finding where the server machine is; if its not on the local */
  12. /* machine ask the user for the right address */
  13.  
  14. /* ============================================================= */
  15. /* |                Include files                   | */
  16. /* ============================================================= */
  17. #include "Address.h"
  18.  
  19. #include "Connect.h"
  20. #include "Client_Ex.h"
  21.  
  22. #include "NetLib:netdb.h"
  23.  
  24. #include "DeskLib:Error.h"
  25. #include "DeskLib:Event.h"
  26. #include "DeskLib:Handler.h"
  27. #include "DeskLib:Icon.h"
  28. #include "DeskLib:Msgs.h"
  29. #include "DeskLib:Window.h"
  30. #include "DeskLib:WIMP.h"
  31.  
  32. #include <string.h>
  33. #include <assert.h>
  34.  
  35. /* ============================================================= */
  36. /* |                    #Definitions                           | */
  37. /* ============================================================= */
  38. #define LOCAL_HOST_NAME "localhost"
  39.  
  40. /* ============================================================= */
  41. /* |                     Structures                            | */
  42. /* ============================================================= */
  43. enum {
  44.   address_TEXT = 2,
  45.   address_OK,
  46.   address_CANCEL
  47. };
  48.  
  49. /* ============================================================= */
  50. /* |              Global variables                   | */
  51. /* ============================================================= */
  52. static window_handle Address_Window = 0;
  53. static char HostAddress[MAXHOSTNAMELEN+1];
  54.  
  55. /* ============================================================= */
  56. /* |                  Function Prototypes                      | */
  57. /* ============================================================= */
  58. static void Address_OpenWindow(void);
  59. static void Address_CloseWindow(void);
  60. static BOOL Address_ClickHandler(event_pollblock *event, void *reference);
  61. static BOOL Address_Delete(event_pollblock *event, void *reference);
  62.  
  63. /* ============================================================= */
  64. /* |                  External functions                       | */
  65. /* ============================================================= */
  66. extern void Address_ContactServer(void)
  67. {
  68. /* first check on local address before we do anything else */
  69.   if(!Connect_ContactServer(LOCAL_HOST_NAME))
  70.   {
  71. /* local server not found, so get from user */
  72.     strcpy(HostAddress, LOCAL_HOST_NAME);
  73.     Address_OpenWindow();
  74.   }
  75. /* otherwise it seems we found the server */
  76. }
  77. /* ============================================================= */
  78. /* |                  Internal functions                       | */
  79. /* ============================================================= */
  80.  
  81. /* open window to ask user for server address */
  82. static void Address_OpenWindow(void)
  83. {
  84.   if(!Address_Window)
  85.   {
  86.     Address_Window = Window_CreateAndShow("GetServAddr", 0, open_CENTERED);
  87.  
  88. /* error occured in getting template */
  89.     if(Address_Window == 0)
  90.     {
  91.       Msgs_Report(0, "no.template");
  92.       Client_Ex_RequestQuit();
  93.       return;
  94.     }
  95.  
  96.     Icon_SetText(Address_Window, address_TEXT, HostAddress);
  97.     Icon_SetCaret(Address_Window, address_TEXT);
  98.  
  99.     Event_Claim(event_CLOSE, Address_Window, event_ANY, Address_Delete,
  100.       &Address_Window);
  101.     Event_Claim(event_OPEN, Address_Window, event_ANY, Handler_OpenWindow,
  102.       NULL);
  103.     Event_Claim(event_CLICK, Address_Window, event_ANY, Address_ClickHandler,
  104.       NULL);
  105.   }
  106. }
  107. static BOOL Address_ClickHandler(event_pollblock *event, void *reference)
  108. {
  109.   /* kill compiler warning */
  110.   UNUSED(reference);
  111.  
  112.   switch(event->data.mouse.icon)
  113.   {
  114.     case address_OK:
  115.       Icon_GetText(Address_Window, address_TEXT, HostAddress);
  116.       Address_CloseWindow();
  117.  
  118.       if(!Connect_ContactServer(HostAddress))
  119.       {
  120.         Address_OpenWindow();
  121.       }
  122.       else
  123.       {
  124.         /* server apparently found at this point */
  125.       }
  126.       break;
  127.  
  128.     case address_CANCEL:
  129.       Address_CloseWindow();
  130.       Client_Ex_RequestQuit();
  131.       break;
  132.   }
  133.  
  134.   return TRUE;
  135. }
  136.  
  137. static void Address_CloseWindow(void)
  138. {
  139.   if(Address_Window)
  140.   {
  141.       Window_Delete(Address_Window);
  142.       Address_Window = 0;
  143.   }
  144. }
  145.  
  146. /* ============================================================= */
  147. /* |                          Notes                            | */
  148. /* ============================================================= */
  149. static BOOL Address_Delete(event_pollblock *event, void *reference)
  150. {
  151.    window_handle *window = reference;
  152.  
  153.    assert(event->data.openblock.window == *window);
  154.    /* make sure handles match each other */
  155.  
  156.    Window_Delete(event->data.openblock.window);
  157.  
  158.    *window = 0;
  159.  
  160.    /* is the window was closed with the cross, kill the program off */
  161.    Client_Ex_RequestQuit();
  162.  
  163.    return TRUE;
  164. }
  165.